package com.goods.business.service.impl;

import cn.hutool.core.convert.Convert;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.goods.business.mapper.*;
import com.goods.business.service.OutStockService;
import com.goods.common.model.business.*;
import com.goods.common.utils.ListPageUtils;
import com.goods.common.vo.business.InStockVO;
import com.goods.common.vo.business.OutStockVO;
import com.goods.common.vo.business.ProductVO;
import com.goods.common.vo.system.PageVO;
import com.google.gson.internal.$Gson$Preconditions;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @Author jiaoyanlin
 * @Date 2021/11/10 17:14
 * @Description OutStockServiceImpl
 */
@Service
public class OutStockServiceImpl implements OutStockService {
    @Autowired
    private OutStockMapper outStockMapper;
    @Autowired
    private ProductStockMapper productStockMapper; //库存表
    @Override
    public PageVO<OutStockVO> findOutStockList(Map<String, Object> map) {
        Integer pageNum = Convert.toInt(map.get("pageNum"), 1);
        Integer pageSize = Convert.toInt(map.get("pageSize"), 10);
        Integer status = Convert.toInt(map.get("status"), 0);
        Object outNum1 = map.get("outNum");
        Object type = map.get("type");

        PageHelper.startPage(pageNum,pageSize);
        Example example =new Example(OutStock.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("status",status);
        String outNum="";
        if (null!=outNum1){
            outNum=Convert.toStr(outNum1);
            criteria.andLike("out_Num","%"+outNum+"%");
        }
        String ty="";
        if (null!=type){
            ty=Convert.toStr(type);
            criteria.andEqualTo("type",ty);
        }
        List<OutStock> outStocks = outStockMapper.selectByExample(example);
        List<OutStockVO> outStockVOS =  outStocks.stream().map(inStock -> {
            OutStockVO outStockVO = new OutStockVO();
            BeanUtils.copyProperties(inStock,outStockVO);
            return outStockVO;
        }).collect(Collectors.toList());

        List<OutStockVO> page = ListPageUtils.page(outStockVOS, pageSize, pageNum);
        PageInfo<OutStockVO> info = new PageInfo<>(outStockVOS);

        return new PageVO<>(info.getTotal(),page);
    }
    @Autowired
    private ConsumerMapper consumerMapper;
    // TODO　多表　添加　　注解这么加　能生效吗？　　能实现一张表失败　其他表都回滚吗
    @Transactional
    @Override
    public Boolean addOutStock(OutStockVO outStockVO) {
        if (null!=outStockVO){
            Long consumerId = outStockVO.getConsumerId();
            if (null!=consumerId){
                return getaBoolean(outStockVO);
            }else {
                //先添加 consumer表 保证consumerId主键 不是空
                Consumer consumer = new Consumer();
                consumer.setCreateTime(outStockVO.getCreateTime());
                consumer.setAddress(outStockVO.getAddress());
                consumer.setContact(outStockVO.getContact());
                consumer.setSort(outStockVO.getSort());
                consumer.setName(outStockVO.getName());
                consumerMapper.insert(consumer);
                outStockVO.setConsumerId(consumer.getId());
                return getaBoolean(outStockVO);
            }


        }
        return false;
    }
  @Autowired
  private OutStockInfoMapper outStockInfoMapper;
    @Autowired
    private ProductMapper productMapper;
    @Override
    public Map<String, Object> detail(Integer id, Integer pageNum) {
        //1.查out_num  在查pum  在查 商品集合
        OutStock outStock = outStockMapper.selectByPrimaryKey(id);
        Example example = new Example(OutStockInfo.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.orEqualTo("outNum",outStock.getOutNum());
        List<OutStockInfo> outStockInfos = outStockInfoMapper.selectByExample(example);
        Map<String, Object> map =new HashMap<>();
        if (!CollectionUtils.isEmpty(outStockInfos)){
            for (OutStockInfo outStockInfo : outStockInfos) {
                String pNum = outStockInfo.getPNum();
                Example o = new Example(Product.class);
                Example.Criteria c = o.createCriteria();
                criteria.orEqualTo("pNum",outStockInfo.getPNum());
                List<Product> products = productMapper.selectByExample(o);
                if (!CollectionUtils.isEmpty(products)){
                    // TODO 这是不是要把集合prodcuts 转成map<>
                    map.put("itemVOS",products);
                    map.put("total",products.size());
                    map.put("status",outStock.getStatus());
                    System.out.println("map = " + map);
                    return map;
                }

                return map;
            }
        }

       return new HashMap<>();

    }

    private Boolean getaBoolean(OutStockVO outStockVO) {
        OutStock outStock = new OutStock();
        BeanUtils.copyProperties(outStockVO,outStock);
        String uuid = UUID.randomUUID().toString();
        outStock.setOutNum(uuid);
        outStock.setStatus(2);
        int insert = outStockMapper.insert(outStock);
        if (insert!=0){
            // TODO　　减库存　　ｐｒｏｄｕｃｔ是什么结构
     /*   //添加成功　　减库存　　操作其他关联表
        Example example = new Example(ProductStock.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo();
        productStockMapper.selectByPrimaryKey();*/
            return true;
        }
        return false;
    }

}
